
  /*
	@Author: 于辉
	@Time: 2020-11
	@Tittle: selectUsers
	@Description: 选中用户列表
*/
layui.use(['layer','jquery',"table"],function(){
	var layer= layui.layer
	    table = layui.table;
	var mainID = getUrlParam("mainID");
	var type =  getUrlParam("type");
	var index = parent.layer.getFrameIndex(window.name); 

	var url = "";
	var data = {};
	switch (type) {
		case "roleUser":
            url = getServerPath("GETEXCLUDEUSER");
            data = {roleid:mainID};
			break;
		case "userDepartment":
			url = getServerPath("GETEXCLUDEUSERINDEPARTMENT");
			break;
    }
	getUserExcludeRoleid()
	//根据roleid获取该角色下未授权的用户列表
  function getUserExcludeRoleid(){
		ajaxSetting({
			url:url,
			type:'get',
			data: data,
			success:function(res){
                sourceData = res.data;
                pageData = res.data;
                let new_data = $.extend(true, [], res.data);
                tableIns=table.render({
                    elem: '#dataTable'
                    ,id: 'dataTable'
                    , loading: true
                    ,height: 'full-150'
                    , autoSort: false //手动排序
                    , page: true
                    , limit: 8
                    , limits:[10,20,30,40]
                    , cols: [[
                        {type:'checkbox',width:80}
                        ,{field:'userID', title: 'ID',hide:true, sort: true}
                        ,{field:'userName', width:150, title: '用户名',align:"center"}
                        ,{field:'nickName', width:150, title: '昵称',align:"center"}
                        ,{field:'phone', width:150, title: '手机号',align:"center"}
                        ,{field:'sex', width:70, title: '性别',align:"center"}
                        ,{field:'enabled', width:150, title: '是否启用',align:"center",templet:function(row){
                            if(row.enabled){
                                return "启用";
                            }else{
                                return "禁用";
                            }

                        }}
                    ]]
                    , data:new_data
                    , done: function () {
                        searchPage();
                    }
                });
			}

		})
	}
	//搜索
	function searchPage() {
	     $("#search_input").keydown(function (e) {
	         var curKey = e.which;
	         if (curKey == 13) {
	             var loading = layer.load(2, {
	                 content:'搜索中...',
	                 shade:  [0.3, '#393D49'],time: 3 * 1000
	             });
	             var Keyword = $('#search_input').val();
	             Keyword = trim(Keyword);
	             pageData = array_search(sourceData, Keyword);
	             let new_data = $.extend(true, [], pageData);
	             table.reload('dataTable', { data: new_data });
	             layer.close(loading);
	             $('#search_input').val(Keyword);
	        }
	     });
	 }
	//取消
	$("#btn_cancle").click(function(){
		parent.layer.close(index)
	})
	//确认
	$("#btn_save").click(function(){
		var checkStatus = table.checkStatus('dataTable');
	 	if(checkStatus.data.length<=0){
	 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
	 		return;
	 	}
	 	var selectIds = new Array();
	 	for(var i=0;i<checkStatus.data.length;i++){
	 		selectIds.push(checkStatus.data[i].userID);
	 	}
        var url = "";
        var data = {};
        switch (type) {
            case "roleUser":
                url = getServerPath("ADDROLESUSER");
                data = {roleId:mainID,userIds:selectIds};
                break;
            case "userDepartment":
                url = getServerPath("UPDATEUSERDEPARTMENT");
                data = {departmentId:mainID,insertUserIds:selectIds,removeUserIds:[]};
                break;
        }
	 	ajaxSetting({
	 		url:url,
	 		data:JSON.stringify(data),
	 		success:function(){
	 			layer.msg("保存成功",{icon:6});
	 			parent.reloadData();


	 			setTimeout(function(){
	 				parent.layer.close(index)
	 			},300)
	 		}
	 	})
		//
	})
});